
#import "Common.h"
#import "PongPrefs.h"

@interface PongPrefs (PrivateAPI)
-(void)prefsDidChange;
@end

@implementation PongPrefs

- (id)init
{
	self = [super init];
	if (nil != self)
	{
		hostName = [[NSString alloc] initWithString:@"localhost"];
		port = 12000;
	}
	return self;
}


//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
	//LogMethod();
    [self setHostName:nil];
	[self setDelegate:nil];
    [super dealloc];
}

-(void)prefsDidChange
{
	//LogMethod();
	if (nil != delegate && [delegate respondsToSelector:@selector(prefsDidChange:)])
		[delegate performSelector:@selector(prefsDidChange:) withObject:self];
}


//NSCoding
- (void)encodeWithCoder:(NSCoder *)coder 
{
	//LogMethod();
    [coder encodeObject:[self hostName] forKey:@"hostName"];
    [coder encodeInt:[self port] forKey:@"port"];
}

- (id)initWithCoder:(NSCoder *)coder 
{
	//LogMethod();
	self = [super init];
    if (nil != self) {
        [self setHostName:[coder decodeObjectForKey:@"hostName"]];
        [self setPort:[coder decodeIntForKey:@"port"]];
    }
    return self;
}


//=========================================================== 
//  hostName 
//=========================================================== 
- (NSString *)hostName
{
    return [[hostName retain] autorelease]; 
}
- (void)setHostName:(NSString *)anHostName
{
    // NSLog(@"in -setHostName:, old value of hostName: %@, changed to: %@", hostName, anHostName);

    if (hostName != anHostName) {
        anHostName = [anHostName copy];
        [hostName release];
        hostName = anHostName;
		[self prefsDidChange];
    }
}

//=========================================================== 
//  port 
//=========================================================== 
- (int)port
{
    //NSLog(@"in -port, returned port = %i", port);

    return port;
}
- (void)setPort:(int)aPort
{
    // NSLog(@"in -setPort, old value of port: %i, changed to: %i", port, aPort);
    port = aPort;
	[self prefsDidChange];
}

//=========================================================== 
//  delegate 
//=========================================================== 
- (id)delegate
{
    return delegate; 
}
- (void)setDelegate:(id)aDelegate
{
	delegate = aDelegate;
}

-(NSString *)description
{
	NSString *desc = [NSString stringWithFormat:@"PongPrefs %x with port = %i and hostName = %@", self, port, hostName];
	return desc;
}

@end